package vesion2.main;



import vesion2.object.special.Orientation;
import vesion2.tank.Bullet;
import vesion2.tank.EnemyTank;
import vesion2.tank.MyTank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.lang.invoke.ConstantCallSite;

public class GamePanel extends JPanel implements KeyListener {
    private ObjectManager manager;
    private RefreshThread refreshThread;
    private ControlPanel cPanel;

    private boolean isStop;

    public GamePanel(){
        super();
        setBackground(Color.BLACK);
        manager=new ObjectManager(this);
        isStop=true;
    }

    public void setControlPanel(ControlPanel cPanel){
        this.cPanel=cPanel;
        if(manager.loadPre())
        {
            cPanel.getHasKill().setText("击杀数: "+manager.getHasKillNum());
            cPanel.getDeathNUm().setText("死亡数: "+manager.getDeathNum());
            cPanel.getJs01().setValue(manager.getAllank());
            cPanel.getJs02().setValue(manager.getMaxTank());
        }else {
            manager.InitData();
            manager.setMaxTank(cPanel.getJs02().getValue());
            manager.setAllank(cPanel.getJs01().getValue());
        }
    }


    /**
     * 重新开始游戏
     * @param all 总敌人数量
     * @param max 同时出现最大坦克数量
     * @param map 地图
     */
    public void reStartGame(int all,int max,String map ){
        pauseGame();
        manager.clear();
        manager.setAllank(all);
        manager.setMaxTank(max);
        if(!manager.loadNew(map))
            manager.InitData();
        manager.getBullets().clear();
        manager.getBombs().clear();
        System.out.println(manager.getBarriers().size()+"___");
        repaint();
    }

    public void pauseGame(){
        isStop=true;
        cPanel.getKeep().setText("继续游戏");
    }

    public void keepGame(){
        isStop=false;
        if(refreshThread==null||!refreshThread.isAlive()) {
            refreshThread = new RefreshThread(this, 40);
            refreshThread.start();
        }
        for(Bullet bullet:manager.getBullets())
            bullet.reStart();
        for(EnemyTank tank:manager.getEnemyTanks())
            tank.reStart();
        manager.createEnemyTank();
    }

    public void saveGame(){
        manager.saveGame();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //画边框
        g.setColor(new Color(0xB3ECCA));
        g.fillRect(0,0,getWidth(),10);
        g.fillRect(0,getHeight()-10,getWidth(),10);
        g.fillRect(0,10,10,getHeight()-20);
        g.fillRect(getWidth()-10,10,10,getHeight()-20);
        //同步数据
        if(cPanel!=null)
        {
            cPanel.getHasKill().setText("击杀数: "+manager.getHasKillNum());
            cPanel.getDeathNUm().setText("死亡数: "+manager.getDeathNum());
        }
        manager.draw(g);
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        MyTank myTank=manager.getMyTank();
        switch (e.getKeyChar()) {
            case 'w':
            case 'W':
                if(isStop)
                    return;
                if (myTank.getOrt() != Orientation.UP)
                    myTank.setOrt(Orientation.UP);
                else
                    myTank.move();
                break;
            case 'a':
            case 'A':
                if(isStop)
                    return;
                if (myTank.getOrt() != Orientation.LEFT)
                    myTank.setOrt(Orientation.LEFT);
                else
                    myTank.move();
                break;
            case 'S':
            case 's':
                if(isStop)
                    return;
                if (myTank.getOrt() != Orientation.DOWN)
                    myTank.setOrt(Orientation.DOWN);
                else
                    myTank.move();
                break;
            case 'd':
            case 'D':
                if(isStop)
                    return;
                if (myTank.getOrt() != Orientation.RIGHT)
                    myTank.setOrt(Orientation.RIGHT);
                else
                    myTank.move();
                break;
            case 'p':
            case 'P':
                if(isStop)
                    return;
                myTank.shitBullet();break;
            default:
                break;
        }
  }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    public ObjectManager getManager(){
        return manager;
    }

    public boolean isStop() {
        return isStop;
    }

}
